package com.sykj.bsc.rwl.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sykj.bsc.rwl.mapper.SysDeptMapper;
import com.sykj.bsc.rwl.po.SysDept;
import com.sykj.bsc.rwl.po.ZtreeBean;
import com.sykj.bsc.rwl.service.SysDeptService;
import com.sykj.bsc.rwl.util.Code;
import com.sykj.bsc.rwl.util.JedisClientPool;
import com.sykj.bsc.rwl.util.JsonUtils;

@Service("sysDeptService")
public class SysDeptServiceImpl implements SysDeptService {
	
	@Autowired
	private SysDeptMapper sysDeptMapper;
	private ZtreeBean ztreeBean;
	/*@Autowired
	private JedisClientPool pool;*/
	
	//查询所有组织名称
	public List<SysDept> selectSysDept(HashMap<String,String> map) throws Exception {
		List<SysDept> list=sysDeptMapper.selectSysDept(map);
		/*String json=pool.get("selectSysDeptMaplistt1");
		if(json==null) {
			list=sysDeptMapper.selectSysDept(map);
			json=JsonUtils.objectToJson(list);
			pool.set("selectSysDeptMaplistt1", json);
		}else {
			if(map!=null) {
				list=sysDeptMapper.selectSysDept(map);
				json=JsonUtils.objectToJson(list);
				pool.set("selectSysDeptMaplist", json);
			}
			list=JsonUtils.jsonToList(json, SysDept.class);
		}*/
		return list;
	}

	@Override
	public String selectAllSysDept(HashMap<String,String> map) throws Exception {
		//String json=pool.get("selectSysDeptMaplistt2");
		List<SysDept> list=sysDeptMapper.selectSysDept(map);
		/*if(json==null) {
			list=sysDeptMapper.selectSysDept(map);
			json=JsonUtils.objectToJson(list);
			pool.set("selectSysDeptMaplistt2", json);
		}else {
			if(map!=null) {
				list=sysDeptMapper.selectSysDept(map);
				json=JsonUtils.objectToJson(list);
				pool.set("selectSysDeptMaplistt2", json);
			}
			list=JsonUtils.jsonToList(json, SysDept.class);
		}*/
		List<ZtreeBean> list2=new ArrayList<ZtreeBean>();
		for(SysDept sysDept:list) {
			ztreeBean=new ZtreeBean();
			ztreeBean.setParentID(Integer.parseInt(sysDept.getParent_id()));
			ztreeBean.setResourceID(sysDept.getDept_id());
			ztreeBean.setResourceName(sysDept.getDept_name());
			ztreeBean.setAccessPath("/bsc/sysDept/selectSysDept?cascade_id="+sysDept.getCascade_id());
			list2.add(ztreeBean);
		}
		String data=JsonUtils.objectToJson(list2);
		return data;
	}

	@Override
	public void insertSysDept(SysDept sysDept) throws Exception {
		HashMap<String, String> map=new HashMap<String,String>();
		HashMap<String, String> map3=new HashMap<String,String>();
		Integer dept_id=sysDept.getDept_id();
		SysDept sys=sysDeptMapper.findByIdSysDept(dept_id);
		/*String json=pool.get("findByIdSysDept"+dept_id+"syst3");
		if(json==null) {
			sys=sysDeptMapper.findByIdSysDept(dept_id);
			json=JsonUtils.objectToJson(sys);
			pool.set("findByIdSysDept"+dept_id+"syst3", json);
		}else {
			sys=JsonUtils.jsonToPojo(json, SysDept.class);
		}*/
		String parent_id=dept_id+"";
		map3.put("a", parent_id);
		int count=sysDeptMapper.findIdCount(map3);
		String cascade_id=sys.getCascade_id();
		String cascade_id2="";
		if(count==0) {
			cascade_id2=Code.getCode(cascade_id, cascade_id);
		}else {
			map.put("cascade_id", cascade_id);
			map.put("parent_id", parent_id);
			SysDept sd=sysDeptMapper.findyBycascade_idSysDept(map);
			/*String json2=pool.get("findyBycascade_idSysDeptMapsdt4");
			if(json2==null) {
				sd=sysDeptMapper.findyBycascade_idSysDept(map);
				json2=JsonUtils.objectToJson(sd);
				pool.set("findyBycascade_idSysDeptMapsdt4", json2);
			}else {
				sd=JsonUtils.jsonToPojo(json2, SysDept.class);
			}*/
			String ChirdCode=sd.getCascade_id();
			cascade_id2=Code.getCode(cascade_id, ChirdCode);
		}
		sysDept.setCascade_id(cascade_id2);
		sysDept.setParent_id(parent_id);
		sysDeptMapper.insertSysDept(sysDept);
	}

	//修改
	public void updateSysDept(SysDept sysDept) throws Exception {
		sysDeptMapper.updateSysDept(sysDept);
	}

	//删除
	public void deleteSysDept(Integer dept_id) throws Exception {
		sysDeptMapper.deleteSysDept(dept_id);
	}

	//查找单个
	public SysDept findByIdSysDept(Integer dept_id) throws Exception {
		SysDept sysDept=sysDeptMapper.findByIdSysDept(dept_id);
		/*String json=pool.get("findByIdSysDept"+dept_id+"t5");
		if(json==null) {
			sysDept=sysDeptMapper.findByIdSysDept(dept_id);
			json=JsonUtils.objectToJson(sysDept);
			pool.set("findByIdSysDept"+dept_id+"t5", json);
		}else {
			if(dept_id!=null) {
				sysDept=sysDeptMapper.findByIdSysDept(dept_id);
				json=JsonUtils.objectToJson(sysDept);
				pool.set("findByIdSysDept"+dept_id+"t5", json);
			}
			sysDept=JsonUtils.jsonToPojo(json, SysDept.class);
		}*/
		return sysDept;
	}
}
